/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConversationView.java
 *
 * Created on Apr 2, 2012, 11:16:18 PM
 */
package org.nightknight.satobee.gui;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import org.nightknight.satobee.core.Conversation;
import org.nightknight.satobee.core.Message;
import org.nightknight.satobee.core.Server;

/**
 *
 * @author nightknight
 */
public class ConversationView extends javax.swing.JFrame {

    /** Creates new form ConversationView */
    public ConversationView() {
        initComponents();
        
        messageList.setEditable(false);
        sidePane.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeButton = new javax.swing.JButton();
        sendButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        message = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageList = new javax.swing.JTextPane();
        sidePane = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        sendFileItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        closeItem = new javax.swing.JMenuItem();
        viewMenu = new javax.swing.JMenu();
        showSidePane = new javax.swing.JCheckBoxMenuItem();
        editMenu = new javax.swing.JMenu();

        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.nightknight.satobee.gui.SatoBeeGUIApp.class).getContext().getActionMap(ConversationView.class, this);
        closeButton.setAction(actionMap.get("quit")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.nightknight.satobee.gui.SatoBeeGUIApp.class).getContext().getResourceMap(ConversationView.class);
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        sendButton.setAction(actionMap.get("sendMessage")); // NOI18N
        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setName("sendButton"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        message.setColumns(20);
        message.setRows(5);
        message.setName("message"); // NOI18N
        jScrollPane2.setViewportView(message);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        messageList.setName("messageList"); // NOI18N
        jScrollPane1.setViewportView(messageList);

        sidePane.setName("sidePane"); // NOI18N

        javax.swing.GroupLayout sidePaneLayout = new javax.swing.GroupLayout(sidePane);
        sidePane.setLayout(sidePaneLayout);
        sidePaneLayout.setHorizontalGroup(
            sidePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 218, Short.MAX_VALUE)
        );
        sidePaneLayout.setVerticalGroup(
            sidePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 401, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N
        menuBar.setOpaque(false);

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        sendFileItem.setAction(actionMap.get("sendFile")); // NOI18N
        sendFileItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        sendFileItem.setText(resourceMap.getString("sendFileItem.text")); // NOI18N
        sendFileItem.setName("sendFileItem"); // NOI18N
        fileMenu.add(sendFileItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        closeItem.setAction(actionMap.get("quit")); // NOI18N
        closeItem.setText(resourceMap.getString("closeItem.text")); // NOI18N
        closeItem.setName("closeItem"); // NOI18N
        fileMenu.add(closeItem);

        menuBar.add(fileMenu);

        viewMenu.setText(resourceMap.getString("viewMenu.text")); // NOI18N
        viewMenu.setName("viewMenu"); // NOI18N

        showSidePane.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        showSidePane.setText(resourceMap.getString("showSidePane.text")); // NOI18N
        showSidePane.setName("showSidePane"); // NOI18N
        showSidePane.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                showSidePaneItemStateChanged(evt);
            }
        });
        viewMenu.add(showSidePane);

        menuBar.add(viewMenu);

        editMenu.setText(resourceMap.getString("editMenu.text")); // NOI18N
        editMenu.setName("editMenu"); // NOI18N
        menuBar.add(editMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sendButton)
                        .addGap(18, 18, 18)
                        .addComponent(closeButton)))
                .addGap(18, 18, 18)
                .addComponent(sidePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sidePane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(sendButton)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    // TODO add your handling code here:
    conv.closeConnection();
}//GEN-LAST:event_formWindowClosing

    private void showSidePaneItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_showSidePaneItemStateChanged
        // TODO add your handling code here:
        sidePane.setVisible(showSidePane.getState());
    }//GEN-LAST:event_showSidePaneItemStateChanged

    @Action
    public void sendMessage() {
        //发送信息
        String text = message.getText();
        
        //将信息添加到显示区域
        Message mes = conv.makeMessage(1, text);
        
        if (!conv.sendMessage(mes)) {
            mes.setContent("message send failed, probably "
                    + "caused by the user you contact with has quit");
        }
        
        addElement(mes);
        
        message.setText(null);
    }

    @Action
    public void sendFile() {
        File selectedFile = null;
        if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(null)) {
            selectedFile = fileChooser.getSelectedFile();
        }
        
        conv.sendFile(selectedFile);
    }

    @Action
    public void quit() {
        conv.closeConnection();
        this.dispose();
    }
    
    public void fileReceived(Message mes) {
        String cont = mes.getContent();
        String text = "The contact try to send a file \"";
        text += cont.substring(1);
        text += "\", would you like to receive it?";
        String file = cont.substring(1);
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
                null, text, "File Received", JOptionPane.YES_NO_OPTION)) {
            if (JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(null)) {
                File rFile = fileChooser.getSelectedFile();
                conv.acceptFileRec(rFile, file);
            }
        } else {
            conv.refuseFileRec(file);
        }
    }
    
    /**
     * 向列表中加入元素
     * @param message 
     */
    public void addElement(Message message) {
        //输出发送者与发送时间
//        model.addElement(message.getSender().getAddresss() + "  " + message.geteSendTime());
        //输出发送内容
//        model.addElement("\t" + message.getContent());
        content += message.getSender().getAddresss() + "  " + message.geteSendTime();
        content += "\n";
        content += "\t" + message.getContent();
        content += "\n";
        
        messageList.setText(content);
    }
    
    public Conversation getConversation() {
        return this.conv;
    }
    
    public void setConversation(Conversation conv) {
        this.conv = conv;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JMenuItem closeItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextArea message;
    private javax.swing.JTextPane messageList;
    private javax.swing.JButton sendButton;
    private javax.swing.JMenuItem sendFileItem;
    private javax.swing.JCheckBoxMenuItem showSidePane;
    private javax.swing.JPanel sidePane;
    private javax.swing.JMenu viewMenu;
    // End of variables declaration//GEN-END:variables

//    private DefaultListModel model;
    private Conversation conv;
    private String content;
    private JFileChooser fileChooser = new JFileChooser();
    private Map<String, Object> fileLocks = new HashMap<String, Object>();
    private Server server = ServerGUIImpl.getServer();
}
